﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALDictionary : DALBase
    {
        public static DALDictionary Singleton = new DALDictionary();

        public GTDictionary Add(GTDictionary dictionary)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newDic = new Dictionary();
                newDic.Code = dictionary.Code;
                newDic.Type = dictionary.Type;
                newDic.Value = dictionary.Value;


                db.Dictionaries.InsertOnSubmit(newDic);
                db.SubmitChanges();

                return dictionary;
            }
        }

        public GTDictionary Update(GTDictionary dictionary)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var newDic = db.Dictionaries.SingleOrDefault(x => x.Code == dictionary.Code && x.Type == dictionary.Type);
                newDic.Code = dictionary.Code;
                newDic.Type = dictionary.Type;
                newDic.Value = dictionary.Value;
             

                db.SubmitChanges();
                return dictionary;
            }
        }




        public List<GTDictionary> GetPaged(DictionaryTypeEnum typeEnum)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Dictionaries
                 where p.Type==(decimal) typeEnum
                 orderby p.Code
                 select p;

                var brands = brds.ToList();
                var ret = new List<GTDictionary>();
                foreach (var brand in brands)
                {
                    GTDictionary gtBrand = new GTDictionary();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public GTDictionary GetPaged(DictionaryTypeEnum typeEnum,string code)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Dictionaries
                 where p.Type == (decimal)typeEnum && p.Code==code
                 orderby p.Code
                 select p;

                var brands = brds.SingleOrDefault();
               
               
                    GTDictionary gtBrand = new GTDictionary();
                    gtBrand.UpdateTo(brands);


                    return gtBrand;
            }
        }
        



        public void Delete(string code, DictionaryTypeEnum typeEnum)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                Dictionary singleOrDefault = db.Dictionaries.SingleOrDefault(x => x.Code == code && x.Type==(decimal) typeEnum);

                if (singleOrDefault != null)
                {
                    db.Dictionaries.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }


       


    }
}
